<?php

class Default_Controllers_Customer extends Libs_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        $this->view->render('customer/index');
    }

    public function register() {
        $this->view->render('customer/register');
    }

    public function registerProcess() {

        $e = $_POST['txtEmail'];
        $p = $_POST['txtPass'];
        $p1 = $_POST['txtPass1'];

        $lname = $_POST['txtLastName'];
        $fname = $_POST['txtFirstName'];
        $sex = $_POST['radSex'];
        $birth = date("Y-m-d", strtotime($_POST['txtBirth']));
        $phone = $_POST['txtPhone'];
        $add = $_POST['txtAddress'];

        if ($p == $p1) {
            $data = array(
                "email" => "$e",
                "password" => Hash::create($p),
                "last_name" => "$lname",
                "first_name" => "$fname",
                "gender" => "$sex",
                "dob" => "$birth",
                "phone" => "$phone",
                "address" => "$add"
            );

            $rp = new Default_Models_Customer();
            $rp->addCus($data);

            header("location: " . URL_BASE . "/customer");
        }
    }

    public function loginProcess() {
        $u = $_POST['txtUser'];
        if (isset($_COOKIE['pass']) && strlen($_POST['txtPass']) > 16) {
            $p = $_COOKIE['pass'];
        } else {
            $p = Hash::create($_POST['txtPass']);
        }
        $lp = new Default_Models_Customer();
        $cus = $lp->checkLogin($u, $p);
        if (!empty($cus)) {
            $_SESSION['user'] = $cus['email'];

            if (isset($_POST['chkRemember'])) {
                setcookie('user', $u, time() + 3600, '/');
                if (isset($_COOKIE['pass']) && $_POST['txtPass'] > 16) {
                    setcookie('pass', $p, time() + 3600, '/');
                } else {
                    setcookie('pass', Hash::create($p), time() + 3600, '/');
                }
            } else {
                setcookie('user', '', time() - 3600, '/');
                setcookie('pass', '', time() - 3600, '/');
            }
//            $redirect_url = (isset($_SESSION['redirect_url'])) ? $_SESSION['redirect_url'] : URL_BASE;
//            unset($_SESSION['redirect_url']);
//            header("Location: $redirect_url", true, 303);
//            exit;

            $url = URL_BASE;
            if (!empty($_SERVER['HTTP_REFERER'])) {
                header("Location: " . $_SERVER['HTTP_REFERER']);
            } else {
                header("Location: $url");
            }
            exit;
//            $this->view->msg = "Đăng nhập thành công";
//            $this->view->render('customer/msg');
        } else {
            header("location: " . $url . "/customer");
        }
    }

    public function logout() {
        if (isset($_SESSION['user'])) {
            unset($_SESSION['user']);
            $url = URL_BASE;
            if (!empty($_SERVER['HTTP_REFERER'])) {
//                header("Location: " . $_SERVER['HTTP_REFERER']);
                echo "<script type='text/javascript'>window.history.go(-2)</script>";
            } else {
                header("Location: $url");
            }
            exit;
        } else {
            $url = URL_BASE;
            if (!empty($_SERVER['HTTP_REFERER'])) {
                header("Location: " . $_SERVER['HTTP_REFERER']);
            } else {
                header("Location: $url");
            }
            exit;
        }
    }

}
